RollDie.java

C:\Users\John\Google Drive\laptop_work\ece538\jhtp_10th\ch07\fig07_07>java RollDie
Face Frequency
   1   1000279
   2   1001606
   3    999272
   4   1000222
   5    999492
   6    999129


RollDie.java

// Fig. 7.7: RollDie.java
// Die-rolling program using arrays instead of switch.
import java.security.SecureRandom;

public class RollDie 
{
   public static void main(String[] args)
   {
      SecureRandom randomNumbers = new SecureRandom();
      int[] frequency = new int[7]; // array of frequency counters

      // roll die 6,000,000 times; use die value as frequency index
      for (int roll = 1; roll <= 6000000; roll++) 
         ++frequency[1 + randomNumbers.nextInt(6)];  

      System.out.printf("%s%10s%n", "Face", "Frequency");
   
      // output each array element's value
      for (int face = 1; face < frequency.length; face++)
         System.out.printf("%4d%10d%n", face, frequency[face]);
   } 
} // end class RollDie


Maintained by John Loomis, updated Tue Jan 24 17:02:43 2017